package com.wclass.common;

import com.google.common.collect.Maps;

import javax.servlet.ServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类
 *
 * @author zhanghaogao
 * @create 2017-11-15 上午10:07
 **/
public class ToolUtil {


    /**
     * 获取职务名称
     * @param roleId
     * @return
     */
    public static String getRoleName(Integer roleId){
        if (roleId == null)
            return "";
        Map<String, String> roleMap = Maps.newHashMap();
        roleMap.put("1", "校长");
        roleMap.put("2", "教务主任");
        roleMap.put("3", "教师");
        roleMap.put("4", "助教");
        roleMap.put("5", "咨询");
        roleMap.put("6", "财务");
        return roleMap.get(roleId + "");
    }

    /**
     * 获取职务描述
     * @param roleId
     * @return
     */
    public static String getRoleRemark(Integer roleId){
        if (roleId == null)
            return "";
        Map<String, String> roleMap = Maps.newHashMap();
        roleMap.put("1", "申请入驻的创始人，拥有所有功能的权限，且可以为其他角色自由配置职位。");
        roleMap.put("2", "协助管理机构，主要是教务教学相关工作的管理（发布课程、课程群主），拥有添加成员，设置教务主任以下职位的权限。");
        roleMap.put("3", "主要负责给学生上课、布置和批改作业，以及准备课程教案和教学资料。");
        roleMap.put("4", "主要协助老师上课，工作内容包括在课堂上进行班级纪律管理，以及维护班级群等。");
        roleMap.put("5", "主要负责课程咨询。");
        roleMap.put("6", "主要负责机构的营收查询管理。");
        return roleMap.get(roleId + "");
    }

    /**
     * 生成随即昵称
     * @return
     */
    public static String GenerateChineseWord() {
        String ChineseWord = "";
        Integer[] countArr = new Integer[]{2,3,4,3,4,2,4,3,4,2};
        Random random = new Random();
        int count = countArr[random.nextInt(10)];
        for (int i = 0; i < count; i++) {
            String str = null;
            int hightPos, lowPos; // 定义高低位
            random = new Random();
            hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
            lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                str = new String(b, "GBK"); // 转成中文
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ChineseWord += str;
        }
        return ChineseWord;
    }

    /**
     * 获取请求参数并转map
     *
     * @param request
     * @return
     */
    public static Map<String, Object> getParametersStartingWith(ServletRequest request) {

        Enumeration paramNames = request.getParameterNames();
        Map<String, Object> params = new TreeMap();
        while ((paramNames != null) && (paramNames.hasMoreElements())) {
            String paramName = (String) paramNames.nextElement();
            String values = request.getParameter(paramName);
            params.put(paramName, values);
        }
        return params;
    }


    public static String filterUtf8mb4(String str) {
        final int LAST_BMP = 0xFFFF;
        StringBuilder sb = new StringBuilder(str.length());
        for (int i = 0; i < str.length(); i++) {
            int codePoint = str.codePointAt(i);
            if (codePoint < LAST_BMP) {
                sb.appendCodePoint(codePoint);
            } else {
                i++;
            }
        }
        return sb.toString();
    }

    /**
     * 随机生成六位数验证码
     *
     * @return
     */
    public static int getRandomNum() {
        Random r = new Random();
        return r.nextInt(900000) + 100000;// (Math.random()*(999999-100000)+100000)
    }

    /**
     * 检测字符串是否不为空(null,"","null")
     *
     * @param s
     * @return 不为空则返回true，否则返回false
     */
    public static boolean notEmpty(String s) {
        return s != null && !"".equals(s) && !"null".equals(s);
    }

    /**
     * 验证手机号码
     * @param mobileNumber
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    public static void main(String[] args) {
        for (int i = 0;i < 20;i++) {
            System.out.println((int) (Math.random() * 100000000 + 100000000));
        }
    }
}
